//
//  ClockViewController.m
//  Clock
//
//  Created by 郭明皓 on 16/6/23.
//  Copyright © 2016年 郭明皓. All rights reserved.
//

#import "ClockViewController.h"
#import "AddViewController.h"
@interface ClockViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *clockTableview;
@property (nonatomic, strong)NSMutableArray *tableArr;
@end

@implementation ClockViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _tableArr = [NSMutableArray array];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"闹钟";
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addclock:)];
    self.clockTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    [self.view addSubview:_clockTableview];
    self.clockTableview.delegate = self;
    self.clockTableview.dataSource = self;
}
//重写系统的编辑按钮触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.clockTableview setEditing:editing animated:animated];
}

//设置可以编辑的操作
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableArr removeObjectAtIndex:indexPath.row];
    [self.clockTableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
#pragma mark table协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *addtableview = @"tableviewId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:addtableview];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:addtableview];
    }
    cell.textLabel.text = self.tableArr[indexPath.row];
    cell.detailTextLabel.text = @"111";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击");
    
}

- (void)addclock:(UIBarButtonItem *)add {
    AddViewController *addView = [[AddViewController alloc]init];
    [self.navigationController pushViewController:addView animated:YES];
//    [self presentViewController:addView animated:YES completion:^{
        addView.times = ^(NSString *str1, NSString *str2) {
            NSString *str = [NSString stringWithFormat:@"%@:%@", str1, str2];
            [self.tableArr addObject:str];
            NSLog(@"哈哈哈哈%@", str);
            [self.clockTableview reloadData];
            return str;
        };

//    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
